import {Injectable} from '@angular/core';
import {TurboperCategory} from '../entity/turboper-category.model';
import {AppConfig} from '../config';
import {Observable} from 'rxjs/Observable';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class TurboperCategoryService {
  private param = 'operCategorys';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  create(turboperCategory: TurboperCategory): Promise<number> {

    return this.http.post(this.url, JSON.stringify(turboperCategory))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return result.code;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  hasOperCategoryCode(operCategoryCode: string, operCategoryId: number): Observable<boolean> {
    const url = `${this.url}/operCategoryCode/${operCategoryCode}/operCategoryId/${operCategoryId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  /**
   * 校验key是否存在
   *
   * @param {string} operCategoryKey
   * @param {number} operCategoryId
   * @returns {Observable<boolean>}
   * @memberof TurboperCategoryService
   */
  hasOperCategoryKey(operCategoryKey: string, operCategoryId: number): Observable<boolean> {

    const url = `${this.url}/operCategoryKey/${operCategoryKey}/operCategoryId/${operCategoryId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  //����turbineName �ж��Ƿ��Ѿ�����
  hasOperCategoryName(operCategoryName: string, operCategoryId: number): Observable<boolean> {
    // turbineName = `${turbineName}`;
    // let turName = turbineName.replace(/^#/, '%23')
    const url = `${this.url}/operCategoryName/${operCategoryName}/operCategoryId/${operCategoryId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  update(turboperCategory: TurboperCategory): Promise<TurboperCategory> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(turboperCategory))
      .toPromise()
      .then(() => turboperCategory)
      .catch(this.handleError);
  }

  batchDelete(valueIds: number[]): Promise<any> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    // const apiUrl = `http://192.168.15.3:8585/webapi/v1/turbines?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return result;
            case '-1':
              throw new Error(result.message);
            case '8001':
              return result;
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  /**
   * 获取子状态列表
   */
  getTurboperCategorys(): Promise<TurboperCategory[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data)
      .catch(this.handleError);
  }

  getTurboperCategorysByGeneralCategory(generalCategory: string): Promise<TurboperCategory[]> {
    return this.http.get(`${this.url}/generalCategory/${generalCategory}`)
      .toPromise()
      .then(response => response.data as TurboperCategory)
      .catch(this.handleError);
  }

  getOperCategorysByTurbStatusValue(turbStatusValue: number): Promise<TurboperCategory[]> {
    //let url=`http://192.168.15.150:8585/webapi/v1/operCategorys`

    return this.http.get(`${this.url}/turbStatusValue/${turbStatusValue}`)
      .toPromise()
      .then(response => response.data as TurboperCategory[])
      .catch(this.handleError);
  }

  /**
   *获取是否统计为true的数据
   *
   * @returns {Promise<TurboperCategory[]>}
   * @memberof TurboperCategoryService
   */
  getTurbCategByIsStatistic(): Promise<TurboperCategory[]> {
    const url = `${this.url}/getIsStatisticList`;
    return this.http.get(url)
      .toPromise()
      .then(response => response.data)
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}


// WEBPACK FOOTER //
// ./src/app/service/turboper-category.service.ts
